package presentationTier.actions;

import businessTier.IMovieFacade;
import businessTier.MovieFacade;
import core.Genre;
import core.Medium;
import core.Movie;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import presentationTier.Global;
import presentationTier.MainForm;
import presentationTier.dialogs.AddMovieDialog;

/**
 *
 * @author vojta
 */
public class AddMovieOkAction implements ActionListener{

    AddMovieDialog dialog;

    public AddMovieOkAction(AddMovieDialog aThis) {
        dialog = aThis;
    }

    public void actionPerformed(ActionEvent e) {
        try {
            String name = dialog.txtName.getText();
            String director = dialog.txtDirector.getText();
            
            int year = 0;
            if (!dialog.txtYear.getText().equals(""))
                year = Integer.parseInt(dialog.txtYear.getText());

            Medium medium = (Medium) dialog.comboBoxMedium.getSelectedItem();
            Genre genre = (Genre) dialog.comboBoxGenre.getSelectedItem();
            Movie movie = new Movie(name, year, director, medium, genre);

            IMovieFacade mf = MovieFacade.getInstance();
            mf.addMovie(movie);
            mf.close();

            Global.refreshTable((MainForm) dialog.getParent());
            
            dialog.dispose();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(dialog, "Incorrect information \n" + ex, "Error", JOptionPane.ERROR_MESSAGE);
        }
        

    }

}
